import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Put,
  UseInterceptors,
  UploadedFile
} from '@nestjs/common'
import * as xlsx from 'xlsx'
import { FileInterceptor } from '@nestjs/platform-express/multer'
import { ApiBody, ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger'
import { NoAuth } from 'src/role/decorators/no-auth.decorator'
import { JobService } from './job.service'
import { CreateJobDto } from './dto/create-job.dto'
import { UpdateJobDto } from './dto/update-job.dto'
import { FResponse } from 'src/utils/reponse'
@NoAuth()
@ApiTags('职位')
@Controller('job')
export class JobController {
  constructor(private readonly JobService: JobService) {}

  @Post()
  @ApiOperation({ summary: '创建职位' })
  @ApiBody({ type: CreateJobDto, description: '职位信息' })
  async create(@Body() createJobDto: CreateJobDto) {
    return FResponse.success(await this.JobService.create(createJobDto))
  }
  @Post('upload')
  @ApiOperation({ summary: '批量创建职位' })
  @UseInterceptors(FileInterceptor('file'))
  async batchCreate(@UploadedFile() file) {
    const fileData = file.buffer
    const workbook = xlsx.read(fileData, { type: 'buffer' })
    const worksheet = workbook.Sheets[workbook.SheetNames[0]]
    const data: CreateJobDto[] = xlsx.utils.sheet_to_json(worksheet)
    await Promise.all(data.map(item => this.JobService.create(item)))
    return FResponse.success('批量创建成功')
  }
  @Post('all')
  @ApiOperation({ summary: '获取所有职位' })
  @ApiBody({
    description: '职位信息'
  })
  async findAll(
    @Body()
    body: {
      page: number
      pageSize: number
      departmentId?: number
      isDelete?: boolean
    }
  ) {
    return FResponse.success(
      await this.JobService.findAll({
        page: +body.page,
        pageSize: +body.pageSize,
        departmentId: +body.departmentId,
        isDelete: body.isDelete
      })
    )
  }
  // 获取真全部职位
  @Get('all/:id')
  @ApiOperation({ summary: '获取真全部职位' })
  async findAllOption(@Param('id') id: number) {
    return FResponse.success(await this.JobService.findAllOption(+id))
  }

  // 获取职位工资
  @Get('salary/:id')
  @ApiOperation({ summary: '获取职位工资' })
  async findSalary(@Param('id') id: number) {
    return FResponse.success(await this.JobService.findSalary(+id))
  }

  @Patch(':id')
  @ApiOperation({ summary: '修改指定职位' })
  @ApiParam({ name: 'id', description: '职位ID' })
  @ApiBody({ type: UpdateJobDto, description: '职位信息' })
  async update(@Param('id') id: string, @Body() updateJobDto: UpdateJobDto) {
    return FResponse.success(await this.JobService.update(+id, updateJobDto))
  }

  // 批量删除
  @Delete()
  @ApiOperation({ summary: '批量删除职位' })
  @ApiBody({ type: [Number], description: '职位ID数组' })
  async batchRemove(@Body() ids: number[]) {
    return FResponse.success(await this.JobService.remove(ids))
  }

  // 彻底删除
  @Delete('thorough')
  @ApiOperation({ summary: '批量彻底删除职位' })
  @ApiBody({ type: [Number], description: '职位ID数组' })
  async batchThoroughRemove(@Body() ids: number[]) {
    return FResponse.success(await this.JobService.thoroughRemove(ids))
  }

  // 批量恢复
  @Put('restore')
  @ApiOperation({ summary: '批量恢复职位' })
  @ApiBody({ type: [Number], description: '职位ID数组' })
  async batchRestore(@Body() ids: number[]) {
    return FResponse.success(await this.JobService.restore(ids))
  }
}
